using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;

namespace SharpMedia.Controls
{
    public class HUDTextBoxInput : HUDContainerSimple
    {
        bool fontNotLoaded = false;
        HUDFont font;
        HUDTextBox textbox;
        HUDMoveList moveList;
        public HUDTextBoxInput(HUDTextBox textbox)
        {
            this.textbox = textbox;
            font = HUDFont.GetFont(textbox.Label.Font);
            HUDImage marker = new HUDImage("white.png");
            marker.Size = Size;
            marker.ConstrainProportions = false;
            marker.Color = ColorS.Green;
            marker.Alpha = 127;
            marker.Anchor = Anchor.All;
   //         Add(marker);

            moveList = new HUDMoveList(new SizeS(font.LineHeight * 2, font.LineHeight), false);
            moveList.Size = Size;
            moveList.Anchor = Anchor.All;
            moveList.StartIndex = moveList.SelectedIndex -2;
            Add(moveList);

            if (!font.IsLoaded)
            {
                fontNotLoaded = true;
                visible = false;
            }
            else
            {
                UpdatePosition(textbox, false);
                AddAlphabet();
                SelectedIndex = 0;
                //moveList.MoveItems(false);
                UpdatePosition(textbox, false);

            }


        }

        public override void ResizeMove(RectangleS oldRect, RectangleS newRect)
        {
            base.ResizeMove(oldRect, newRect);
            SelectedIndex = SelectedIndex;
        }

        public char GetChar()
        {
            return moveList.GetSelectedControl<HUDLabel>().Text[0];
        }

        public void SelectChar(char c)
        {
            for (int i = 0; i < moveList.Count; i++)
			{
                HUDLabel l = (HUDLabel)moveList[i];
                if (l.Text.ToString() == c.ToString())
                {
                   SelectedIndex = i;
                   return;
                }
			}
           
        }

        public int SelectedIndex
        {
            get { return moveList.SelectedIndex; }
            set {
                if (moveList.Count == 0)
                    return ;
                if (moveList.SelectedIndex < 0)
                    moveList.SelectedIndex = 0;
                HUDLabel label = moveList.Get<HUDLabel>(moveList.SelectedIndex);
                if (label != null)
                {
                    label.Color = ColorS.White;
                }
                else
                {
                }
                moveList.SelectedIndex = value; 
                moveList.StartIndex = moveList.SelectedIndex - 2;
                label = moveList.Get<HUDLabel>(moveList.SelectedIndex);
                if (label != null)
                {
                    label.Color = ColorS.Black;
                }
                else
                {
                }
            }
        }


        public override void Update(TimeSpan ts)
        {
            if (fontNotLoaded && font.IsLoaded)
            {
                UpdatePosition(textbox, false);
                visible = true;
                fontNotLoaded = false;
                AddAlphabet();
                SelectedIndex = 0;
                moveList.MoveItems(false);

            }
            base.Update(ts);
        }
        private void AddAlphabet()
        {
            char alphaStart = Char.Parse("a");
            char alphaEnd = Char.Parse("z");
            for(char i = alphaStart; i <= alphaEnd; i++)
            {
                HUDLabel letter = new HUDLabel(i.ToString(), font.Name);
                letter.Size = moveList.ItemSize;
                letter.CharacterOffset = textbox.Label.CharacterOffset;
                letter.Alignment = Alignment.Center | Alignment.VCenter;
                moveList.Add(letter);
            }

            alphaStart = Char.Parse("0");
            alphaEnd = Char.Parse("9");
            for (char i = alphaStart; i <= alphaEnd; i++)
            {
                HUDLabel letter = new HUDLabel(i.ToString(), font.Name);
                letter.Size = moveList.ItemSize;
                letter.CharacterOffset = textbox.Label.CharacterOffset;
                letter.Alignment = Alignment.Center | Alignment.VCenter;
                moveList.Add(letter);
            }
        }

        public void UpdatePosition(HUDTextBox textbox, bool animate)
        {
            moveList.ItemSize = new SizeS(font.LineHeight * 2, font.LineHeight);
            DestinationSize = new SizeS(font.LineHeight * 2, font.LineHeight * 5);
            Destination = new PointS(textbox.Bounds.Left + textbox.GetTextWidth() - font.LineHeight / 2f - font.PadLeft, textbox.Bounds.Bottom - textbox.Bounds.Height / 2f - DestinationSize.Y / 2f +font.PadUp);
            if (!animate)
            {
                Size = DestinationSize;
                Location = Destination;
            }

        }
    }
}
